package com.weichuanbo.xwdz.mvp.model;

import android.app.Application;

import com.google.gson.Gson;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;
import com.weichuanbo.xwdz.app.utils.SignUtil;
import com.weichuanbo.xwdz.mvp.contract.UserContract;
import com.weichuanbo.xwdz.mvp.model.api.RetrofitHelper;
import com.weichuanbo.xwdz.mvp.model.api.service.ApiServer;
import com.weichuanbo.xwdz.mvp.model.entity.BaseResponse;
import com.weichuanbo.xwdz.mvp.model.entity.UserBean;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;


@ActivityScope
public class UserModel extends BaseModel implements UserContract.Model {
    @Inject
    Gson mGson;
    @Inject
    Application mApplication;

    @Inject
    public UserModel(IRepositoryManager repositoryManager) {
        super(repositoryManager);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
    }

    @Override
    public Observable<Object> getCode(String phone) {
        //使用rxcache缓存,上拉刷新则不读取缓存,加载更多读取缓存
        Map<String, String> map = new HashMap<>();
        map.put("phone",phone);
        SignUtil.addSign(map);//添加签名
       /* return Observable.just(mRepositoryManager
                .obtainRetrofitService(ApiServer.class)
                .getValidateCode(map))
                .flatMap(new Function<Observable<Object>, ObservableSource<Object>>() {
                    @Override
                    public ObservableSource<Object> apply(@NonNull Observable<Object> listObservable) throws Exception {
                        return mRepositoryManager.obtainCacheService(CommonCache.class)
                                .getUsers(listObservable, new DynamicKey(lastIdQueried), new EvictDynamicKey(update))
                                .map(listReply -> listReply.getData());
                    }
                });*/
        return mRepositoryManager.obtainRetrofitService(ApiServer.class).getValidateCode(map).map(new RetrofitHelper.HttpResultFuct<>());

    }

    @Override
    public Observable<UserBean> login(String phone, String verifycode, String type) {
        //使用rxcache缓存,上拉刷新则不读取缓存,加载更多读取缓存
        Map<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("msg_code", verifycode);
        map.put("login_type", type);
        //添加签名
        SignUtil.addSign(map);
        Observable<BaseResponse<UserBean>> login = mRepositoryManager.obtainRetrofitService(ApiServer.class).login(map);
        return login.map(new RetrofitHelper.HttpResultFuct<>());

    }
}